import socket

HOST = 'localhost'  # 监听IP地址，为本机地址
PORT = 8888  # 监听端口号

with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
    s.bind((HOST, PORT))  # 绑定IP地址和端口号
    s.listen()  # 开始监听

    print(f"Server is listening at {HOST}:{PORT}...")

    conn, addr = s.accept()  # 接受客户端连接
    with conn:
        print(f"Connected by {addr}")
        while True:
            data = conn.recv(1024)  # 接收数据
            if not data:
                break
            print(f"Received: {data.decode('utf-8')}")
            conn.sendall(data)  # 发送数据回应客户端
